package com.jack.helloworld.activity;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.jack.helloworld.R;
import com.jack.helloworld.adapter.AudioRecorderAdapter;
import com.jack.helloworld.bean.Recorder;
import com.jack.helloworld.manager.MediaManager;
import com.jack.helloworld.view.AudioRecorderButton;

import java.util.ArrayList;
import java.util.List;

public class AudioRecorderActivity extends AppCompatActivity {

    private AudioRecorderButton mButtonAudioRecorder;
    private View mViewAnim;

    private ListView mListViewAudioRecorder;
    private AudioRecorderAdapter mAudioRecorderAdapter;
    private List<Recorder> mDatas = new ArrayList<Recorder>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_recorder);

        initView();
        initData();
    }

    private void initData() {

        mButtonAudioRecorder.setOnAudioFinishRecorderListener(new AudioRecorderButton.OnAudioFinishRecorderListener() {
            @Override
            public void audioFinishRecorder(float time, String filePath) {
                Recorder recorder = new Recorder();
                recorder.setTime(time);
                recorder.setFilePath(filePath);
                mDatas.add(recorder);
                mAudioRecorderAdapter.notifyDataSetChanged();
            }
        });

        mAudioRecorderAdapter = new AudioRecorderAdapter(this, mDatas);
        mListViewAudioRecorder.setAdapter(mAudioRecorderAdapter);

        mListViewAudioRecorder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //停止播放的动画
                if (mViewAnim != null) {
                    mViewAnim.setBackgroundResource(R.drawable.audio_recorder_adj);
                    mViewAnim = null;
                }

                //播放动画
                mViewAnim = view.findViewById(R.id.view_audio_recorder_anim);
                mViewAnim.setBackgroundResource(R.drawable.anim_audio_recoder_play);
                AnimationDrawable animationDrawable = (AnimationDrawable) mViewAnim.getBackground();
                animationDrawable.start();

                //播放音频
                MediaManager.playSound(mDatas.get(position).getFilePath(), new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mViewAnim.setBackgroundResource(R.drawable.audio_recorder_adj);
                    }
                });
            }
        });
    }

    private void initView() {
        mButtonAudioRecorder = (AudioRecorderButton) findViewById(R.id.btn_audio_recorder);
        mListViewAudioRecorder = (ListView) findViewById(R.id.lv_audio_recorder);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MediaManager.pause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MediaManager.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MediaManager.release();
    }
}
